﻿using System;
using System.Collections;
using System.Web;
using System.Web.Caching;

namespace wojilu
{
    public class DefaultCacheStrategy : ICacheStrategy
    {
        protected int _timeOut = 3600;
        private static object syncObj = new object();
        protected static volatile Cache webCache = HttpRuntime.Cache;

        public virtual void AddObject(string string_0, object object_0)
        {
            if (((string_0 != null) && (string_0.Length != 0)) && (object_0 != null))
            {
                if (this.TimeOut == 7200)
                {
                    webCache.Insert(string_0, object_0, null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.High, null);
                }
                else
                {
                    webCache.Insert(string_0, object_0, null, DateTime.Now.AddSeconds((double)this.TimeOut), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                }
            }
        }

        public virtual void AddObject(string string_0, object object_0, int int_0)
        {
            if (((string_0 != null) && (string_0.Length != 0)) && (object_0 != null))
            {
                if (int_0 == 0)
                {
                    webCache.Insert(string_0, object_0, null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.High, null);
                }
                else
                {
                    webCache.Insert(string_0, object_0, null, DateTime.Now.AddSeconds((double)int_0), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
                }
            }
        }

        public virtual void AddObjectWithDepend(string string_0, object object_0, string[] string_1)
        {
            if (((string_0 != null) && (string_0.Length != 0)) && (object_0 != null))
            {
                CacheItemRemovedCallback onRemoveCallback = new CacheItemRemovedCallback(this.onRemove);
                CacheDependency dependencies = new CacheDependency(null, string_1, DateTime.Now);
                webCache.Insert(string_0, object_0, dependencies, DateTime.Now.AddSeconds((double)this.TimeOut), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemoveCallback);
            }
        }

        public virtual void AddObjectWithFileChange(string string_0, object object_0, string[] string_1)
        {
            if (((string_0 != null) && (string_0.Length != 0)) && (object_0 != null))
            {
                CacheItemRemovedCallback onRemoveCallback = new CacheItemRemovedCallback(this.onRemove);
                CacheDependency dependencies = new CacheDependency(string_1, DateTime.Now);
                webCache.Insert(string_0, object_0, dependencies, DateTime.Now.AddSeconds((double)this.TimeOut), Cache.NoSlidingExpiration, CacheItemPriority.High, onRemoveCallback);
            }
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        public virtual void FlushAll()
        {
            IDictionaryEnumerator enumerator = HttpRuntime.Cache.GetEnumerator();
            while (enumerator.MoveNext())
            {
                webCache.Remove(enumerator.Key.ToString());
            }
        }

        public void onRemove(string string_0, object object_0, CacheItemRemovedReason cacheItemRemovedReason_0)
        {
        }

        public virtual void RemoveObject(string string_0)
        {
            if ((string_0 != null) && (string_0.Length != 0))
            {
                webCache.Remove(string_0);
            }
        }

        public virtual object RetrieveObject(string string_0)
        {
            if ((string_0 != null) && (string_0.Length != 0))
            {
                return webCache.Get(string_0);
            }
            return null;
        }

        public static Cache GetWebCacheObj
        {
            get
            {
                return webCache;
            }
        }

        public virtual int TimeOut
        {
            get
            {
                if (this._timeOut <= 0)
                {
                    return 3600;
                }
                return this._timeOut;
            }
            set
            {
                this._timeOut = (value > 0) ? value : 3600;
            }
        }
    }
}
